'use strict'

const titbit = require('titbit')
const { cookie, session } = require('titbit-toolkit')


const app = new titbit({
    debug: true
})

//先cookie后session，不是同时出现
app.use(new cookie)
    .use(new session)


app.use(async (c, next) => {
    c.setHeader('x-test-key', `${Math.random()}`)
    await next()
})
//middleware
//c.routepath
app.use(async (c, next) => {
    if (c.query.say === '12345') {
        await next()
    } else {
        c.status(403).send('deny')
    }
}, { name: ['home'], method: ['GET'] })//针对home的GET才会执行

app.get('/', async c => {
    c.send('ok')
}, { name: 'home', group: 'home' })//中英文都可(c.name  c.group)==>

app.get('/test', async c => {
    c.send('test is ok')
})
// app.get('/:x/:y', async c => {
//     c.setSession('x', c.param.x)
//     c.setSession('y', c.param.y)
//     c.send(c.getSession())
// })

app.get('/token', async c=> {
    let tk = tok.make({
        id: '123',
        user: 'node',
        level: 12
    })
    c.send(tk)
})
app.get('/decrypt/:token', async c => {
    c.send(tok.verify(c.param.token))
})

app.run(2021)
